<%
    new_post = %w[new create].include? action_name
    path = new_post ? '/posts' : post_path(@post)
    method = new_post ? 'push' : 'put'
%>

<% if @post.errors.any? %>
    <div class="admonitionblock important">
        <table>
            <tbody>
            <tr>
                <td class="icon">
                    <i class="fa icon-important" title="Important"></i>
                </td>
                <td class="content">
                    <div class="title">The post had <%= pluralize(@post.errors.count, 'error') %>:
                    </div>
                    <div class="ulist">
                        <ul>
                            <% @post.errors.full_messages.each do |message| %>
                                <li><%= message %></li>
                            <% end %>
                        </ul>
                    </div>
                </td>
            </tr>
            </tbody>
        </table>
    </div>
<% end %>

<%= form_tag path, method: method, class: 'pure-form pure-form-stacked new-post' do %>
    <fieldset>
        <legend>
            Create new post. Body of the post is processed by
            <a href="http://asciidoctor.org">asciidoctor</a>.
        </legend>

        <div class="pure-g">
            <div class="pure-u-11-24">
                <label for="body">Body</label>
                <%= text_area_tag(:body, @post.body, class: 'pure-input-1') %>
            </div>
            <div class="pure-u-1-24"></div>

            <div class="pure-u-1-2">
                <%= render partial: 'post' %>
            </div>
        </div>


        <% if new_post %>
            <button type="submit" formaction="<%= new_post_path %>" class="pure-button">Preview</button>
            <button type="submit" class="pure-button pure-button-primary">Create</button>
        <% else %>
            <button type="submit" formaction="<%= edit_post_path(@post) %>" class="pure-button">Preview</button>
            <button type="submit" class="pure-button pure-button-primary">Update</button>
        <% end %>

    </fieldset>
<% end %>
